package com.sie.config;

import com.hustcad.plm.pdm.common.service.remote.config.LicenseConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@Configuration
@Primary
@Profile("LicenseConfig")
public class MyLicenseConfig extends LicenseConfig {


    @Bean(
        name = {"localLicenseBackup"}
    )
    public LicenseBean getLicenseBean() {
        Class<?> aClass = null;
        try {
            aClass = Class.forName("com.hustcad.plm.pdm.common.service.remote.config.LicenseConfig$LicenseBean");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 由于构造函数是私有的，我们需要设置访问权限
        Constructor<?> constructor = null;
        try {
            constructor = aClass.getDeclaredConstructor();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        constructor.setAccessible(true); // 使私有构造函数可访问

        // 创建内部类的实例
        LicenseBean innerInstance = null;
        try {
            innerInstance = (LicenseBean) constructor.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return innerInstance;
    }

}
